--
-- Author: ping.wang
-- Date: 2019-05-15 12:35:09
--
-- DailyworkflowView
--

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)
local DailyTimelinePanel = import("..views.DailyTimelinePanel")
local DailyRoutinePanel = import("..views.DailyRoutinePanel")
local TabsBtn = require("app.widget.TabsBtn")

ClassRef.VIEW_ROOT_RES = Res.DailyworkflowScene

function ClassRef:init()

    self:onOperate(self._data.operate)
    self._ui.Button_close:addClickEventListener(function()
        self:closeView()
    end)

    --右侧tab选择按钮

    local btns = {}
    for i = 1,3 do
        btns[i] = self._ui.Panel_tab:getChildByName("selected_btn_"..i)
    end

    if self._data and self._data.selectedIdx then
        self._selectedIdx = self._data.selectedIdx
    else
        self._selectedIdx = 1
    end

    local function onTabsBtn(index, btn)
        self:_refreshViewByIndex(index)
        self._selectedIdx = index
    end

    self._tabsBtn = TabsBtn.new(btns, onTabsBtn, self._selectedIdx)
end

function ClassRef:_refreshViewByIndex(idx)
    self._ui.time:setVisible(false)
    self._ui.pursuit:setVisible(false)
    self._ui.pursuit_week:setVisible(false)
    if idx == 1 then
        if not self._timelinePanel then
            self._timelinePanel = DailyTimelinePanel.create(self._ui.time)
            --请求服务器拿取数据 暂时不做
            --executeFunc(self._onOperate, "getTimelineDataFromServer")
        end
        self._timelinePanel:setVisible(true)

    elseif idx == 2 then
        if not self._dailyRoutinePanel then
            self._dailyRoutinePanel = DailyRoutinePanel.createWithData(1,self._ui.pursuit)
            --请求服务器拿数据 打开界面才需要请求 来回切换不请求
            executeFunc(self._onOperate, "getDailyRoutine")
        end
        self._dailyRoutinePanel:setVisible(true)
    elseif idx == 3 then
        if not self._weekRoutinePanel then
            self._weekRoutinePanel = DailyRoutinePanel.createWithData(2,self._ui.pursuit_week)
            --请求服务器拿数据 打开界面才需要请求 来回切换不请求
            executeFunc(self._onOperate, "getWeekRoutine")
        end
        self._weekRoutinePanel:setVisible(true)

    end
end

function ClassRef:refreshTimelinePanel(data)
    if self._selectedIdx == 1 then
        if self._timelinePanel then
            self._timelinePanel:refreshDataFromServer(data)
        end
    elseif self._selectedIdx == 2 then
        if self._dailyRoutinePanel then
            self._dailyRoutinePanel:refreshDataFromServer(data)
        end
    elseif self._selectedIdx == 3 then
        if self._weekRoutinePanel then
            self._weekRoutinePanel:refreshDataFromServer(data)
        end
    end
end

function ClassRef:reOpen()
    if self._selectedIdx == 2 then
        if self._dailyRoutinePanel then
            executeFunc(self._onOperate, "getDailyRoutine")
        end
    elseif self._selectedIdx == 3 then
        if self._weekRoutinePanel then
            executeFunc(self._onOperate, "getWeekRoutine")
        end
    end
end

return ClassRef
